package cn.nawang.ebeim.api.comm;

import java.util.Arrays;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * @ClassName: Pages
 * @Description 分页类
 * @author 许炜毅
 * @date 2015年8月5日 下午4:39:29
 * @version v1.0
 */
public class Pages {

  private int startPos = 0;
  private int pageSize = 10;
  private int totalPage = 1;
  private int totalRecord = 0;
  // 分页查询条件
  @JsonIgnore
  private SearchFormParams[] searchFormParams;

  public Pages() {
    super();
  }

  /**
   * 初始化对象时设置每页显示的大小
   */
  public Pages(int startPos, int pageSize) {
    if (pageSize == 0) {
      this.startPos = 0;
      this.pageSize = getTotalRecord();
    } else {
      this.startPos = startPos == 1 ? 0 : startPos;
      this.pageSize = pageSize;
    }

  }

  /**
   * 根据总记录数，得到总页数
   */
  public int getAllPage(int allRecord) throws Exception {
    int allPage = 1;
    if (pageSize == 0) {
      return allPage;
    }

    if (allRecord % pageSize == 0) {
      allPage = allRecord / pageSize;
    } else {
      allPage = allRecord / pageSize + 1;
    }
    return allPage;
  }

  /**
   * 根据当前页数，得到每次查询的开始记录数
   */
  public void getStartRecord(int curPage) {
    Integer cur = (curPage - 1) * pageSize;
    this.setStartPos(cur < 0 ? 0 : cur);
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public int getStartPos() {
    Integer cur = (startPos - 1) * pageSize;
    return cur < 0 ? 0 : cur;
  }

  public void setStartPos(int startPos) {
    this.startPos = startPos;
  }

  public int getTotalPage() {
    return totalPage;
  }

  public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
  }

  public int getTotalRecord() {
    return totalRecord;
  }

  public void setTotalRecord(int totalRecord) {
    this.totalRecord = totalRecord;
  }

  public SearchFormParams[] getSearchFormParams() {
    return searchFormParams;
  }

  public void setSearchFormParams(SearchFormParams[] searchFormParams) {
    this.searchFormParams = searchFormParams;
  }

  @Override
  public String toString() {
    return "Pages [startPos=" + startPos + ", pageSize=" + pageSize + ", totalPage=" + totalPage
        + ", totalRecord=" + totalRecord + ", searchFormParams=" + Arrays.toString(searchFormParams)
        + "]";
  }

}
